%%%---------------------------------------------------------------------------------------
%%% @author     Stuart Jackson <simpleenigma@gmail.com> [http://erlsoft.org]
%%% @copyright  2006 - 2007 Simple Enigma, Inc. All Rights Reserved.
%%% @doc        ERML extention manager. This module is used to find and validate gen_erml modules that are lsited in the config files
%%% @reference  See <a href="http://erlsoft.org/modules/erlweb" target="_top">Erlang Software Framework</a> for more information
%%% @reference See <a href="http://erlweb.googlecode.com" target="_top">ErlWeb Google Code Repository</a> for more information
%%% @version    0.0.2
%%% @since      0.0.2
%%% @end
%%%
%%%
%%% The MIT License
%%%
%%% Copyright (c) 2007 Stuart Jackson, Simple Enigma, Inc. All Righs Reserved
%%%
%%% Permission is hereby granted, free of charge, to any person obtaining a copy
%%% of this software and associated documentation files (the "Software"), to deal
%%% in the Software without restriction, including without limitation the rights
%%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
%%% copies of the Software, and to permit persons to whom the Software is
%%% furnished to do so, subject to the following conditions:
%%%
%%% The above copyright notice and this permission notice shall be included in
%%% all copies or substantial portions of the Software.
%%%
%%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
%%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
%%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
%%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
%%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
%%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
%%% THE SOFTWARE.
%%%
%%%
%%%---------------------------------------------------------------------------------------
-module(erml_ext).
-author('sjackson@simpleenigma.com').
-include("../include/erml.hrl").

-export([all/0,all/1,tags/1,verify/1,exports/1,behavior/1,behavior/2]).



all() ->
	all([erml_core,erml_beta,erml_erlmail,erml_erldir,erml_erlvoip,erml_eif]).
all(Modules) ->
	List = lists:map(fun(M) -> verify(M) end,Modules),
	lists:flatten(List).



verify(Module) when is_atom(Module) ->
	lists:foldl(fun(Command,Acc) -> 
			[{Command,Module}|Acc]
		end,[],tags(Module)).
	


tags(Module) ->
	case exports(Module) of
		{error,_Reason} -> [];
		Exports -> 
			lists:foldl(fun({Fun,Args},Acc) -> 
				S = atom_to_list(Fun),
				case {string:left(S,4),Args} of
					{"erml",2} -> [Fun|Acc];
					_ -> Acc
				end
				
				end,[],Exports)
	end.

exports(Module) ->
	case code:get_object_code(Module) of
		{Module,_Obj,BeamPath} -> 
			case beam_lib:chunks(BeamPath,[exports]) of
				{ok,{_Mod,[{exports,Exports}]}} -> Exports;
				{error,_BeamLib,Reason} -> {error,Reason}
			end;
		error -> {error,module_not_loaded}
	end.

behavior(Module) ->
	case catch (Module:module_info(attributes)) of
		{'EXIT',_} -> {error,module_not_found};
		Attributes -> 
			case lists:keysearch(behavior,1,Attributes) of
				{value,{behavior,Behaviors}} -> Behaviors;
				_Other -> {error,no_behavior_defined}
			end
	end.

behavior(Module,Behavior) ->
	case behavior(Module) of
		{error,Reason} -> {error,Reason};
		Behaviors -> lists:member(Behavior,Behaviors)
	end.
